package com.jmebananas.renderer.pass;

import java.util.logging.Level;
import java.util.logging.Logger;

import javacream.resource.ResourceException;
import javacream.resource.ResourceManager;

import com.jme.image.Texture;
import com.jme.renderer.Renderer;
import com.jme.renderer.pass.Pass;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.Text;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jme.util.geom.Debugger;
import com.jmebananas.game.state.AdvancedGameState;
import com.jmebananas.util.Constants;
import com.jmebananas.util.UniqueName;

/**
 * DebugRenderPass
 * 
 * @author Glenn Powell
 *
 */
public class DebugRenderPass extends AdvancedRenderPass {

	private static final long serialVersionUID = 1L;
	
	
	private static final Logger logger = Logger.getLogger(DebugRenderPass.class.getName());
	
	
	private AdvancedGameState state;
	
	private Text debugText;
	
	private boolean renderBounds = false;
	
	
	public DebugRenderPass(AdvancedGameState state) {
		this(state, DEBUG_RENDERPASS_INDEX);
	}
	
	public DebugRenderPass(AdvancedGameState state, int index) {
		super(index);
		
		this.state = state;
		
		AlphaState alphaState = DisplaySystem.getDisplaySystem().getRenderer().createAlphaState();
		alphaState.setBlendEnabled(true);
		alphaState.setSrcFunction(AlphaState.SB_SRC_ALPHA);
		alphaState.setDstFunction(AlphaState.DB_ONE_MINUS_SRC_ALPHA);
		alphaState.setTestEnabled(false);
		alphaState.setEnabled(true);

		Node debugNode = new Node(UniqueName.createName("debug"));
		debugNode.setRenderState(alphaState);

		Texture fontTexture = null;
		try {
			fontTexture = TextureManager.loadTexture(ResourceManager.getURL(Constants.DEFAULT_FONT_PATH),
					Texture.MM_LINEAR, Texture.FM_LINEAR);
		} catch (ResourceException e) {
			logger.log(Level.SEVERE, "Error loading font for Default Font", e);
		}
		
		TextureState textureState = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
		if (fontTexture != null)
			textureState.setTexture(fontTexture);
		textureState.setEnabled(true);

		debugText = new Text(UniqueName.createName("debugText"), "");
		debugText.setRenderState(textureState);
		debugNode.attachChild(debugText);

		debugNode.updateGeometricState(0.0f, true);
		debugNode.updateRenderState();
	}
	
	public void doRender(Renderer renderer) {
		super.doRender(renderer);
		
		if (renderBounds) {
			AdvancedPassManager passManager = state.getPassManager();
			for (int i = 0; i < passManager.passes(); ++i) {
				Pass pass = passManager.get(i);
				if (pass != this) {
					for (int j = 0; j < pass.size(); ++j) {
						Spatial spatial = pass.get(j);
						Debugger.drawBounds(spatial, renderer, true);
					}
				}
			}
		}
		
		renderer.draw(debugText);
	}
	
	public void setDebugText(String text) {
		debugText.print(text);
	}

	public boolean isRenderBounds() {
		return renderBounds;
	}
	
	public void setRenderBounds(boolean renderBounds) {
		this.renderBounds = renderBounds;
	}

}
